<?php
namespace app\api\controller\pub;

use app\api\controller\mytool\Mytool;
use app\api\controller\mytool\Pagr;
use app\api\controller\mytool\Wechat;
use app\common\model\company\Contract;
use app\common\model\company\ContractDetails;
use app\common\model\power\Rolepowerlnk;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use Firebase\JWT\JWT;
use think\facade\Cache;
use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\orderhawa\HawaList;
use app\common\model\buss\Buss;

class Xwprint extends Base
{
    /**
     * 产生jwt加密串，参考session
     */
    public function initialize()
    {
        parent::initialize();
    }

    public function getmm(){
    	$data = Pub::getjwtkeyarray(2);
    	return myok($data);
    }

    /**
     * 打印产废联单
     * @return [type] [description]
     */
    public function orderhawa(){
        $id = input('id');
        $sbdata = HawaList::where('OrderPassword',$id)->find();
        if(empty($sbdata)){
           return moerr('没有找到关联订单'); 
        }
        $data = HawaList::getinfo($sbdata['HawaListId'],'print');
        return myok($data);
    }

	/**
	 * 打印合同
	 * @return \think\response\Json
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function printContract()
	{
		$param = $this->request->get();
		if (empty($param['p'])) {
			return myerr('非法请求');
		}
		$contract = Contract::where('PrintPassword', $param['p'])->findOrEmpty();
		if ($contract->isEmpty()) {
			return myerr('暂无此合同');
		}
		$company = Company::get($contract['CompanyId']);
		$contract['buss'] = UserModel::getpublicinfo($contract['BussinessId']);
		$contract['other'] = ['Name'=> COMPANYALLNAME,'ContactPhone'=>CELLPHONE,'Address'=>COMPANYADDRESS];
		$contract['company'] = ['Name'=>$company['Name'],'Address'=>$company['Address'],'UserName'=>$company['UserName'],'UserTel'=>$company['UserTel']];
		$contract['ContractDetails'] = ContractDetails::where('ContractId',$contract->ContractId)->with('goodinfo')->select();
		$text = Pagr::contract();
		if(!$text['code']){
			return myerr($text['msg']);
		}
		$contract['text'] = $text['msg'];
		$contract['con'] = [
			'CONTRACT_BANKNAME'=>CONTRACT_BANKNAME,
			'CONTRACT_BANKNUMBER'=>CONTRACT_BANKNUMBER,
			'CONTRACT_BANKNUM'=>CONTRACT_BANKNUM,
			'CONTRACT_SHUIHAO'=>CONTRACT_SHUIHAO,
			'CONTRACT_TEL'=>CONTRACT_TEL,
			'COMPANY_LEGAL_USER'=>COMPANY_LEGAL_USER,
			'COMPANYALLNAME'=>COMPANYALLNAME,
			'COMPANYADDRESS'=>COMPANYADDRESS,
			'COMPANYLON'=>COMPANYLON,
			'COMPANYLAT'=>COMPANYLAT,
			'COMPANYNUMBERCARD'=>COMPANYNUMBERCARD,
		];
		$contract['validateurl'] = Wechat::qrcode('validate_contract_'.SITEPERFIX.'_'.$contract['ContractId'],'ticket');
		
		//合同打印的判断逻辑
		$contract['contract_print_control'] = true;
		if(get_config_value('contract_print_control')=='success'&&$contract['State']!==Contract::STATEOK){
			$contract['contract_print_control'] = false;
		}
		
		return myok($contract);
	}

	/**
	 * 扫码验证合同 二维码地址
	 * @return [type] [description]
	 */
	public function vContract(){
		$p = input('p');
		$url = Wechat::qrcode('validate_contract_'.SITEPERFIX.'_'.$p);
		return myok($url);
	}

	/**
	 * 扫码验证收集员信息
	 * @return [type] [description]
	 */
	public function buss(){
		$id = input('id');
		if(empty($id)){
			return myerr('没有找到您需要的业务员信息');
		}
        $data = Buss::user_getinfo($id);
        if(empty($data['State'])){
        	$data['State'] = Buss::STATERETURN;
        }
        $data['CELLPHONE'] = CELLPHONE;
        return myok($data);
	}
}
